package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysMailConf;
import com.ruoyi.system.service.ISysMailConfService;
import com.ruoyi.system.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMailMapper;
import com.ruoyi.system.domain.SysMail;
import com.ruoyi.system.service.ISysMailService;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-28
 */
@Service
public class SysMailServiceImpl implements ISysMailService 
{
    @Resource
    private SysMailMapper sysMailMapper;

    @Override
    public Long selectMailCount(SysMail sysMail) {
        Long aLong = sysMailMapper.selectMailCount(sysMail);
        return aLong;
    }

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysMail selectSysMailById(String id)
    {
        return sysMailMapper.selectSysMailById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sysMail 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysMail> selectSysMailList(SysMail sysMail)
    {
        return sysMailMapper.selectSysMailList(sysMail);
    }


    @Resource
    private MailService mailService;
    @Resource
    private ISysMailConfService sysMailConfService;

    /**
     * 新增【请填写功能名称】
     * 
     * @param sysMail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysMail(SysMail sysMail)
    {
        sysMail.setCreateTime(DateUtils.getNowDate());
        //获取发件人信息
        SysMailConf sysMailConf = new SysMailConf();
        sysMailConf.setUserId(sysMail.getUserId());
        List<SysMailConf> sysMailConfList = sysMailConfService.selectSysMailConfList(sysMailConf);
        if (sysMailConfList != null && sysMailConfList.size() > 0) {

        } else {
            throw new RuntimeException("email config error");
        }
        sysMail.setFromMail(sysMailConfList.get(0).getUserName());
        List<String> filePathList = new ArrayList<>();
        if (!StringUtils.isBlank(sysMail.getFilePath())) {
            filePathList = Arrays.asList(sysMail.getFilePath().split(","));
        }
        mailService.sendAppendixMail(
                sysMailConfList.get(0),
                sysMail.getToMail(),
                sysMail.getTitle(),
                sysMail.getContent(),
                filePathList);
        return sysMailMapper.insertSysMail(sysMail);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sysMail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysMail(SysMail sysMail)
    {
        sysMail.setUpdateTime(DateUtils.getNowDate());
        return sysMailMapper.updateSysMail(sysMail);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysMailByIds(String[] ids)
    {
        return sysMailMapper.deleteSysMailByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysMailById(String id)
    {
        return sysMailMapper.deleteSysMailById(id);
    }
}
